﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Tosi
{
    public partial class MainForm : Form
    {
        private DataFile df = null;
        private Simulation sim;
        private GeneticAlgorithm genAlg;
        private Thread simThread;


        public MainForm()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            df = new DataFile();
            Var.gr = new GraficPlot();
            df.Read(out Var.JncNumber, out Var.JncArr, out Var.JncName,
                out Var.StrNumber, out Var.StrArr, out Var.StrName);
            Var.gr.LoadData(Var.JncNumber, Var.JncArr, Var.JncName, 
                Var.StrNumber, Var.StrArr, Var.StrName);
            ExitBtt.Enabled = false;
        }

        private void PlotConfBtt_Click(object sender, EventArgs e)
        {
            MapConfForm MCForm = new MapConfForm();
            MCForm.ShowDialog();
        }

        private void SimConfBtt_Click(object sender, EventArgs e)
        {
            SimEvalConfForm SECForm = new SimEvalConfForm();
            SECForm.ShowDialog();
        }

        private void PlotBtt_Click(object sender, EventArgs e)
        {
            this.ControlBox = false;
            PlotBtt.Enabled = false;
            MapConfBtt.Enabled = false;
            LoadBtt.Enabled = false;
            SaveBtt.Enabled = false;
            SimConfBtt.Enabled = false;
            Var.gr.LoadData(Var.JncNumber, Var.JncArr, Var.JncName,
                Var.StrNumber, Var.StrArr, Var.StrName);
            Var.gr.openFig();
            Var.gr.scaling();
            Var.gr.changeToBack();
            Var.gr.juntionTemplate();
            Var.gr.junctionNames();
            Var.gr.streets();
            Var.gr.lights();
            Var.gr.scale();
            sim = new Simulation();
            DemoBtt.Enabled = true;
            ExitBtt.Enabled = true;
        }

        private void ExitBtt_Click(object sender, EventArgs e)
        {
            Var.gr.clearFig();
            PlotBtt.Enabled = true;
            MapConfBtt.Enabled = true;
            LoadBtt.Enabled = true;
            SaveBtt.Enabled = true;
            SimConfBtt.Enabled = true;
            ExitBtt.Enabled = false;
            DemoBtt.Enabled = false;
            this.ControlBox = true;
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            df.Write(Var.JncNumber, Var.JncArr, Var.JncName,
                Var.StrNumber, Var.StrArr, Var.StrName);
        }

        private void DemoBtt_Click(object sender, EventArgs e)
        {
            if (Var.On == 1)
            {
                DemoBtt.Text = "Start Demo";
                sim.Stop();
                DemoBtt.Enabled = false;
                ExitBtt.Enabled = true;
                EvalBtt.Enabled = true;
            }
            else
            {
                EvalBtt.Enabled = false;
                ExitBtt.Enabled = false;
                DemoBtt.Text = "Stop Demo";
                simThread = new Thread(sim.Start);
                simThread.Start();
            }
        }

        private void EvalBtt_Click(object sender, EventArgs e)
        {
            DemoBtt.Enabled = false;
            Enabled = false;
            genAlg = new GeneticAlgorithm();
            Enabled = true;
        }

    }
}
